<template>
	<view>
		<view class="head-box">
			<!-- 搜索框 -->
			<view class="sch-head">
				<input v-model="cont" placeholder="请输入您的院校" />
				<image src="/static/search.png" mode=""></image>
				<button v-if="cont!==''">搜索</button>
				<view v-else></view>
			</view>
			<view class="box-shadow"></view>
			<!-- 顶部导航栏 -->
			<top-tabbar 
				:tabBars="tabBars" 
				:tabIndex="tabIndex" 
				@toggleToptab="toggleTab">
			</top-tabbar>
		</view>
		
		<!-- 下方内容 -->
		<view class="content">
			<!-- #ifndef MP-WEIXIN-->
			<swiper :current="tabIndex" @change="tabChange" :style="{ height: app_height * 2 + 'upx' }">
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN-->
			<swiper :current="tabIndex" @change="tabChange" style="height: 1200upx">
			<!-- #endif -->
				<!-- 本科 -->
				<swiper-item>
					<view class="u-menu-wrap">
						<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop" :scroll-into-view="itemId">
							<view v-for="item,index in list" :key="index" class="u-tab-item" :class="[current == index ? 'u-tab-item-active' : '']" @tap.stop="swichMenu(index)">
								<text class="u-line-1">{{ item.school_name }}</text>
							</view>
						</scroll-view>
						<scroll-view :scroll-top="scrollRightTop" scroll-y scroll-with-animation class="right-box" @scroll="rightScroll">
							<view class="page-view">
								<view class="class-item" :id="'item' + index" v-for="item,index in list" :key="index">
									<view class="item-img">
										<image src="/static/zsSchool.png" mode=""></image>
									</view>
									<view class="item-container">
										<view>
											<view class="thumb-box"  @click="schDetailClk(item.id)">
												<image class="item-menu-image" src="/static/logo.png" mode=""></image>
												<view class="item-menu-name1">
													<view class="i-m-name">{{ item.school_name }}</view>
													<view class="i-dz-sc">
														<text style="margin-right: 30rpx;">点赞100</text>
														<text>收藏100</text>
													</view>
												</view>
											</view>
											<view style="padding: 30rpx 30rpx;">
												<view style="height: 2rpx; width: 100%; background: #dddddd;"></view>
											</view>
										</view>
									</view>
								</view>
							</view>
						</scroll-view>
					</view>
				</swiper-item>
				<!-- 大专 -->
				<swiper-item>
					<view class="" v-if="tabIndex == 1">
						<view v-for="(item,index) in 100" :key="index">
							大专
						</view>
					</view>
				</swiper-item>
				<!-- 中专 -->
				<swiper-item>
					<view class="" v-if="tabIndex == 2">
						2
					</view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	import topTabbar from '@/components/top-tabbar.vue'
	export default {
		components:{
			topTabbar
		},
		data() {
			return {
				cont:'',
				tabIndex: 0,
				tabBars: [
					{
						name: "本科",
						id: 0
					}, {
						name: "大专",
						id: 1
					}, {
						name: "中专",
						id: 2
					}
				],
				arr: [],
				menuItemPos: [],
				oldScrollTop: 0,
				timer: null, // 定时器
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				scrollTop: 0, //tab标题的滚动条位置
				menuItemHeight: 0, // 左边菜单item的高度
				itemId: '', // 栏目右边scroll-view用于滚动的id
				scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
				// list: [
				// 	{
				// 		leftName:  '北京大学',
				// 		icon: '/static/logo.png',
				// 		schoolList:[]
				// 	}
				// ],
				list:[],
				app_height: 0
			}
		},
		onLoad() {
			this.getSchool()
			uni.getSystemInfo({
				success: (res) => {
					// 
					this.app_height = res.windowHeight
					console.log("uni:",res)
					console.log("height:",this.app_height)
				}
			})
		},
		onReady() {
			this.getMenuItemTop()
		},
		methods: {
			// 获取所有学校
			async getSchool(){
				const res = await this.get('/index/Index/get_all_school')
				this.list = res.data
			},
			// 学历导航栏 点击跳转
			toggleTab(index) {
				this.tabIndex = index
			},
			// 学历导航栏 左右滑动
			tabChange(e) {
				this.tabIndex=e.detail.current
			},
			schDetailClk(id){
				uni.getStorage({
				    key: 'auth',
				    success: (e) => {
						uni.navigateTo({
							url:'./alumni_circle?school_id='+id
						})
				    },
					fail: (res) => {
						uni.showToast({
							title:"请登录您的账号",
							duration:1000,
							icon:'none',
							success: () => {
								setTimeout(() => {
								    uni.navigateTo({
								    	url:'../login/login'
								    })
								},1000)
							}
						})
					}
				});
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if(this.arr.length == 0) {
					await this.getMenuItemTop()
				}
				if (index == this.current) return;
				this.scrollRightTop = this.oldScrollTop
				this.$nextTick(() => {
					this.scrollRightTop = this.arr[index]
					this.current = index
					this.leftMenuStatus(index)
				})
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this)
					query.select('.' + elClass).fields({ size: true }, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass)
							}, 10)
							return
						}
						this[dataVal] = res.height
						resolve()
					}).exec()
				})
			},
			// 观测元素相交状态
			async observer() {
				this.list.map((val, index) => {
					let observer = uni.createIntersectionObserver(this)
					// 检测右边scroll-view的id为itemxx的元素与right-box的相交状态
					// 如果跟.right-box底部相交，就动态设置左边栏目的活动状态
					observer.relativeTo('.right-box', { top: 0 }).observe('#item' + index, res => {
						if (res.intersectionRatio > 0) {
							let id = res.id.substring(4)
							this.leftMenuStatus(id)
						}
					})
				})
			},
			// 设置左边菜单的滚动状态
			async leftMenuStatus(index) {
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight')
					await this.getElRect('u-tab-item', 'menuItemHeight')
				}
				// 将菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2
			},
			// 获取右边菜单每个item到顶部的距离
			getMenuItemTop() {
				new Promise(resolve => {
					let selectorQuery = uni.createSelectorQuery();
					selectorQuery.selectAll('.class-item').boundingClientRect((rects) => {
						// 如果节点尚未生成，rects值为[](因为用selectAll，所以返回的是数组)，循环调用执行
						if(!rects.length) {
							setTimeout(() => {
								this.getMenuItemTop()
							}, 10)
							return 
						}
						rects.forEach((rect) => {
							// 这里减去rects[0].top，是因为第一项顶部可能不是贴到导航栏(比如有个搜索框的情况)
							this.arr.push(rect.top - rects[0].top)
							resolve()
						})
					}).exec()
				})
			},
			// 右边菜单滚动
			async rightScroll(e) {
				this.oldScrollTop = e.detail.scrollTop;
				if(this.arr.length == 0) {
					await this.getMenuItemTop()
				}
				if(this.timer) return
				if(!this.menuHeight) {
					await this.getElRect('menu-scroll-view', 'menuHeight')
				}
				setTimeout(() => { // 节流
					this.timer = null;
					// scrollHeight为右边菜单垂直中点位置
					let scrollHeight = e.detail.scrollTop + this.menuHeight / 2
					for (let i = 0; i < this.arr.length; i++) {
						let height1 = this.arr[i]
						let height2 = this.arr[i + 1]
						// 如果不存在height2，意味着数据循环已经到了最后一个，设置左边菜单为最后一项即可
						if (!height2 || scrollHeight >= height1 && scrollHeight < height2) {
							this.leftMenuStatus(i)
							return
						}
					}
				}, 10)
			}
		}
	}
</script>

<style scoped lang="scss">
	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
		height: 1200upx;
		.u-tab-view {
			width: 212rpx;
			height: 100%;
			text-align: center;
			background: #F5F5F5;
			padding: 60rpx 20upx 0 50upx;
			.u-tab-item {
				display: flex;
				align-items: center;
				justify-content: center;
				// width: 212rpx;
				font-size: 30rpx;
				margin-bottom: 56upx;
				box-sizing: border-box;
			}
			.u-tab-item-active {
				color: #101010;
				font-size: 30rpx;
				font-weight: bold;
				border-left: 6rpx solid #3291F8;
			}
		}
		.right-box {
			.page-view {
				/* #ifdef H5 */
				padding-bottom: 78rpx;
				/* #endif */
				.class-item {
					background-color: #fff;
					.item-img {
						padding: 14upx 28upx 26upx 30upx;
						image{
							width: 480upx;
							height: 140upx;
						}
					}
					.item-container {
						.thumb-box {
							display: flex;
							align-items: center;
							padding: 0 30rpx;
							.item-menu-image {
								width: 140rpx;
								height: 140rpx;
								border-radius: 50%;
							}
							.item-menu-name1 {
								margin-left: 30rpx;
								.i-m-name{
									color: #4d4d4d;
									font-size: 28rpx;
									margin-bottom: 26rpx;
								}
								.i-dz-sc{
									color: #7d7d7d;
									font-size: 24rpx;
								}
							}
						}
					}
				}
				.class-item:last-child {
					min-height: 100vh;
				}
			}
		}
	}
	.head-box{
		/* #ifndef APP-PLUS-NVUE */
		position: -webkit-sticky;
		/* #endif */
		position: sticky;
		top: var(--window-top);
		z-index: 99;
		.sch-head{
			display: flex;
			align-items: center;
			width: 750upx;
			height: 150upx;
			padding: 0 30upx;
			background: #FFFFFF;
			// box-shadow: 0 2upx 10upx 0 rgba(0, 0, 0, 0.3);
			input{
				width: 100%;
				height: 80upx;
				color: #505559;
				font-size: 28upx;
				background: #EBEBEC;
				padding-left: 80upx;
				border-radius: 10upx;
				position: relative;
			}
			image{
				width: 45upx;
				height: 45upx;
				position: absolute;
				left: 50upx;
				top: 55upx;
			}
			button{
				width: 120upx;
				height: 72upx;
				color: #FFFFFF;
				font-size: 28upx;
				background: #3291F8;
				border-radius: 15upx;
				position: absolute;
				right: 35upx;
				top: 40upx;
			}
		}
		.box-shadow{
			width: 100%;
			height: 10upx;
			background: #3291F8;
			background-image: linear-gradient(to bottom, #d1d1d1, #ffffff);
		}
	}
</style>